import { defineStore } from "pinia";

export const useSystemStore = defineStore({
    id: "system",
    state: () => ({
        theme: localStorage.getItem("darkTheme") ? true : false,
        loading: false,
    }),
    getters: {
        sysTheme() {
            return this.theme;
        },
    },
    actions: {
        //切换主题
        toggleTheme() {
            let darkTheme = localStorage.getItem("darkTheme");
            if (!darkTheme) localStorage.setItem("darkTheme", "yes");
            else localStorage.setItem("darkTheme", "");
            // this.theme = !this.theme;
            let _theme = this.theme;
            if (!_theme) {
                document.documentElement.setAttribute("theme-mode", "dark");
            } else {
                document.documentElement.removeAttribute("theme-mode");
            }
            this.$patch({
                theme: !_theme,
            });
        },

        setLoading(val) {
            this.$patch({
                loading: val,
            });
        },
    },
});
